﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using Service_monitoring.Utils.MailHelper.HelperView;
using Service_monitoring.Utils.MailHelper.Telegram;
using System;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types;

namespace Service_monitoring.Utils.MailHelper.MailHelper
{
    internal class TelegramInformation
    {
        /// <summary>
        ///主服务模块
        /// </summary>
        public static ITelegramBotClient LocalbotClient = null;

        /// <summary>
        /// 系统委托事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static async void Bot_OnMessage(object sender, MessageEventArgs e)
        {
            if (e.Message.Text != null)
            {
                Console.WriteLine($"Received a text message in chat {e.Message.Chat.Id}:{e.Message.Text}");
            }
        }

        /// <summary>
        ///停止service
        /// </summary>
        public static void StopSeivive()
        {
            if (LocalbotClient != null)
            {
                try
                {
                    LocalbotClient.StopReceiving();
                }
                catch (Exception ex)
                {
                    PrintLog.E(ex);
                }
            }
        }

        /// <summary>
        /// 启动service
        /// </summary>
        public static void initSeivive()
        {
            if (LocalbotClient == null)
            {
                try
                {
                    LocalbotClient = new TelegramBotClient(TelegramInfo.GetToken());

                    var me = LocalbotClient.GetMeAsync().Result;
                    Console.WriteLine($"Hello,System Start!");

                    LocalbotClient.OnMessage += Bot_OnMessage;
                    LocalbotClient.StartReceiving();

                    //  LocalbotClient.StopReceiving();
                }
                catch (Exception ex)
                {
                    PrintLog.E(ex);
                }
            }
        }

        /// <summary>
        ///发送信息
        /// </summary>
        /// <param name="text"></param>
        /// <param name="chat"></param>
        public static void SendMessage(string text, ChatId chat)
        {
            Task ThisTask = Task.Run(() =>
            {
                initSeivive();
                try
                {
                    LocalbotClient.SendTextMessageAsync(
            chatId: chat,
            text: text + "-time:" + DateTime.Now);
                }
                catch (Exception ex)
                {
                    PrintLog.E(ex);
                }
            });
        }
    }
}